package model

type User struct {
	Id            int          `json:"id" gorm:"column:id"`
	ProjectID     int          `json:"project_id" gorm:"column:project_id"`
	Role          int          `json:"role" gorm:"column:role"`
	Tel           string       `json:"tel" gorm:"column:tel"`
	Username      string       `json:"username" gorm:"column:username" binding:"required,max=30"`
	Realname      string       `json:"realname" gorm:"column:realname"`
	Password      string       `json:"password" gorm:"column:password"`
	Gender        int8         `json:"gender" gorm:"column:gender"`
	Age           int8         `json:"age" gorm:"column:age"`
	Email         string       `json:"email" gorm:"column:email"`
	Avatar        string       `json:"avatar" gorm:"column:avatar"`
	Desc          string       `json:"desc" gorm:"column:desc"`
	Status        int8         `json:"status" gorm:"column:status"`
	LoginType     int8         `json:"login_type" gorm:"login_type"`
	LastLoginTime *string      `json:"last_login_time" gorm:"column:last_login_time"`
	CreateTime    *string      `json:"create_time" gorm:"column:create_time"`
	UpdateTime    *string      `json:"update_time" gorm:"column:update_time"`
	RoleInfo      AuthRoleSlim `json:"role_info" gorm:"foreignKey:Role;references:Id"`
}

type UserSlim struct {
	Id        int    `json:"id" gorm:"column:id"`
	ProjectID int    `json:"project_id" gorm:"column:project_id"`
	Role      int    `json:"role" gorm:"column:role"`
	Username  string `json:"username" gorm:"column:username" binding:"required,max=30"`
	Status    int8   `json:"status" gorm:"column:status"`
}

func (t *User) TableName() string {
	return "user"
}

func (t *UserSlim) TableName() string {
	return "user"
}

func (t *User) Exists(id int) (exist bool, err error) {
	err = DB.Where("id = ?", id).Limit(1).Find(t).Error
	if err != nil {
		return
	}
	if t.Id == 0 {
		return
	}
	exist = true
	return
}

type UserToken struct {
	Id           int     `json:"id" gorm:"column:id"`
	Uid          int     `json:"uid" gorm:"column:uid"`
	ProjectID    int     `json:"project_id" gorm:"column:project_id"`
	Token        string  `json:"token" gorm:"column:token"`
	Ip           string  `json:"ip" gorm:"column:ip"`
	LoginType    uint8   `json:"login_type" gorm:"column:login_type"`
	CreateTime   *string `json:"create_time" gorm:"column:create_time"`
	RefreshTimes int     `json:"refresh_times" gorm:"column:refresh_times"`
}

func (t *UserToken) TableName() string {
	return "token"
}

type AuthRole struct {
	Id            int     `json:"id" gorm:"column:id"`
	ProjectID     int     `gorm:"column:project_id" json:"project_id"`
	Name          string  `json:"name" gorm:"column:name" binding:"required,max=30"`
	Desc          string  `json:"desc" gorm:"column:desc" binding:"max=255"`
	Status        int8    `json:"status" gorm:"column:status"`
	Route         string  `json:"route" gorm:"column:route"`
	RouteContent  string  `json:"route_content" gorm:"column:route_content"`
	Route2        string  `json:"route2" gorm:"column:route2"`
	RouteContent2 string  `json:"route_content2" gorm:"column:route_content2"`
	PageReport    uint8   `json:"page_report" gorm:"column:page_report"`
	CreateTime    *string `json:"create_time" gorm:"column:create_time"`
	UpdateTime    *string `json:"update_time" gorm:"column:update_time"`
	UserList      []*User `json:"user_list" gorm:"-"`
}

type AuthRoleSlim struct {
	Id   int    `json:"id" gorm:"column:id"`
	Name string `json:"name" gorm:"column:name"`
}

func (t AuthRole) TableName() string {
	return "auth_role"
}

func (t AuthRoleSlim) TableName() string {
	return "auth_role"
}

func (t *AuthRole) Exists(id int) (exist bool, err error) {
	err = DB.Where("id = ?", id).Limit(1).Find(t).Error
	if err != nil {
		return
	}
	if t.Id == 0 {
		return
	}
	exist = true
	return
}

type AuthRoute struct {
	ID   int    `json:"id" gorm:"column:id"`
	Name string `json:"name" gorm:"column:name" binding:"required,max=30"`
	Desc string `json:"desc" gorm:"column:desc" binding:"max=255"`
}

func (t AuthRoute) TableName() string {
	return "auth_route"
}
